package com.mcintyret.site.web.controller.journal;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.mcintyret.site.journal.Timeseries;
import com.mcintyret.site.journal.database.JournalAccessException;
import com.mcintyret.site.service.journal.TimeseriesHelper;
import com.mcintyret.site.utils.TimeseriesGson;
import com.mcintyret.site.web.controller.journal.forms.TimeseriesForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * User: mcintyret2
 * Date: 05/04/2012
 */

@Controller
public class TimeseriesController extends BaseJournalController {

    private final static Logger LOG = LoggerFactory.getLogger(TimeseriesController.class);

    @Resource
    private TimeseriesGson timeseriesGson;
    @Resource
    private TimeseriesHelper timeseriesHelper;


    @RequestMapping(value = "/{origin}/add-timeseries", method = RequestMethod.POST)
    private String addTextEntryToJournal(
            @Valid TimeseriesForm timeseriesForm,
            BindingResult bindingResult,
            @PathVariable String origin) {

        String redirectUrl = "redirect:/" + origin;

        if (!bindingResult.hasErrors()) {
            int id = timeseriesHelper.addNewTimeseriesToJournal(timeseriesForm);
            redirectUrl += "#timeseries_" + id;
        }
        return redirectUrl;
    }

    @RequestMapping("/{origin}/delete-timeseries")
    private String deleteTextEntry(
            @RequestParam("id") int timeseriesId,
            @PathVariable String origin) {

        timeseriesHelper.deleteTimeseriesWithId(timeseriesId);

        return "redirect:/" + origin;
    }

    @RequestMapping("/timeseries")
    public String getTimeseriesPage(
            @RequestParam("id") int id,
            @RequestParam(required = false, value = "compare") String compareIds,
            Model model) {
        setupCommonElements(model);
        Timeseries timeseries = journalDao.getTimeseriesById(id);

        List<Timeseries> timeseriesToChart = Lists.newArrayList(timeseries);
        addComparisonsToChartList(compareIds, timeseriesToChart);

        model.addAttribute("jsonTimeseries", timeseriesGson.toJson(timeseriesToChart));
        model.addAttribute("timeseries", timeseries);
        return "timeseries";
    }

    private void addComparisonsToChartList(String compareIds, List<Timeseries> timeseriesToChart) {
        try {
            if (compareIds != null && !compareIds.equals(ValueConstants.DEFAULT_NONE)) {
                List<Timeseries> timeseriesesToCompare = Lists.newLinkedList(Iterables.transform(
                        StringUtils.commaDelimitedListToSet(compareIds),
                        new Function<String, Timeseries>() {
                            @Override
                            public Timeseries apply(String id) {
                                return journalDao.getTimeseriesById(Integer.parseInt(id));
                            }
                        }));
                timeseriesToChart.addAll(timeseriesesToCompare);
            }
        } catch (JournalAccessException jae) {

        } catch (NumberFormatException nfe) {

        }
    }

}
